<template>
	<!-- 空登陆页 -->
	<view class="login">
		<button class="ss-reset-button avatar-btn"
			open-type="chooseAvatar"
			@chooseavatar="onChooseAvatar">
			<view class="avatar">
				<image :src="avatar || '/static/avatar.png'"></image>
			</view>
		</button>
		<view class="form-input">
			<input placeholder="请输入昵称"
				type="nickname"
				v-model="nickname" />
		</view>
		<view class="btn">
			<button class="ss-reset-button"
				@click="onLogin">
				<view class="con">
					<!-- <image src="/static/img/wx.png"
						class="icon"></image> -->
					<view>登录</view>
				</view>
			</button>
		</view>
	</view>
</template>
<script>
	import {
		mapState,
		mapGetters,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				nickname: "",
				avatar: '',
				avatarUrl: '',
				code: '',
				loading: false
			}
		},
		computed: {
			...mapState(["userInfo", ]),
		},
		onLoad() {
			
		},
		methods: {
			...mapActions(['setUserInfo']),
			async onChooseAvatar(e) {
				let file = e.detail.avatarUrl || '';
				let result = await this.$http.upload("upload", {
					name: 'file',
					filePath: file,
					header: {
						'content-type': 'multipart/form-data'
					},
				})
				if (result.data.code == 0) {
					this.avatar = result.data.data.url
					this.avatarUrl = result.data.data.url
				}
			},
			async onLogin() {
				let that = this
				if (!this.avatar) {
					uni.showToast({
						title: "请设置头像",
						icon: "none"
					})
					return false
				}
				if (!this.nickname) {
					uni.showToast({
						title: "请设置昵称",
						icon: "none"
					})
					return false
				}
				if(this.loading) return false
				this.loading = true
				uni.login({
					success: async (loginResult) => {
						that.$http.get("login", {
							params: {
								code: loginResult.code,
								avatar: that.avatar,
								nickname: that.nickname
							}
						}).then((result)=>{
							if (result.data.code == 0) {
								that.setUserInfo(result.data.data)
								let pages = getCurrentPages()
								if (pages.length > 1) {
									uni.navigateBack()
								} else {
									uni.reLaunch({
										url: "/pages/index/index"
									})
								}
							}
							this.loading = false
						}).catch((err)=>{
							console.log(err)
							this.loading = false
						})
						
						
					},
				})
				
			},
			// 使用微信手机号
			async getPhoneNumber(e) {
				let that = this
				if (e.detail.errMsg !== 'getPhoneNumber:ok') {
					console.log(e.detail.errMsg)
				} else {
					let res = await this.$lib.$http.post({
						url: this.$lib.$urlMap.bindMobile,
						data: {
							encryptedData: e.detail.encryptedData,
							iv: e.detail.iv,
							code: that.code,
							avatar: that.avatarUrl,
							nickname: that.nickname
						},
						needLogin: true
					})
					if (res.code == 1) {
						this.$lib.getLocation()
						that.setUserInfo(res.data)
						let pages = getCurrentPages()
						if (pages.length > 1) {
							uni.navigateBack()
						} else {
							uni.reLaunch({
								url: "/pages/sign/index"
							})
						}
					}
				}
			}
		},
	}
</script>
<style lang="scss"
	scoped>
	/deep/ .uni-easyinput__content {
		background: unset !important;
	}

	/deep/ .uni-easyinput__content-input {
		height: 100rpx !important;
		font-size: 32rpx !important;
	}

	.login {
		width: 100%;
		background: #fff;
		min-height: 100vh;
		/* #ifdef H5 */
		min-height: calc(100vh - 90rpx);

		/* #endif */
		.form-input {
			background: #f6f6f6;
			margin: 0 80rpx;
			height: 100rpx;
			border-radius: 10rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			padding: 0 20rpx;
		}

		.avatar {
			padding: 100rpx 30rpx 30rpx;
			text-align: center;

			image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 50%;
			}
		}

		.btn {
			padding: 200rpx 80rpx 0;

			button {
				height: 104rpx;
				background: #e94142;
				border-radius: 137rpx 137rpx 137rpx 137rpx;
				display: flex;
				flex-direction: column;
				justify-content: center;
				font-size: 36rpx;
				font-family: Source Han Sans-Regular, Source Han Sans;
				font-weight: 400;
				color: #ffffff;

				.con {
					display: flex;
					align-items: center;

					.icon {
						width: 40rpx;
						height: 40rpx;
						margin-right: 20rpx;
					}
				}
			}
		}
	}
</style>